package com.softcloud.gamesbase;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.util.List;

/**
 * Created by 喆歆 on 2015/4/21.
 */
public class GameAdapter extends ArrayAdapter<Game> {

    private int resourceId;

    public GameAdapter(Context context, int textViewResourceId, List<Game> objects) {
        super(context, textViewResourceId, objects);
        resourceId = textViewResourceId;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Game game = getItem(position);
        View view;
        ViewHolder viewHolder = new ViewHolder();
        if(convertView == null){
            view = LayoutInflater.from(getContext()).inflate(resourceId,null);
            viewHolder.gameImage = (ImageView)view.findViewById(R.id.game_image);
            viewHolder.gameName = (TextView)view.findViewById(R.id.game_name);
            view.setTag(viewHolder);
        }else {
            view = convertView;
            viewHolder = (ViewHolder)view.getTag();
        }
        viewHolder.gameImage.setImageResource(game.getImageId());
        viewHolder.gameName.setText(game.getName());
        return view;
    }

    class ViewHolder{
        ImageView gameImage;
        TextView gameName;
    }
}
